<?php

class SupplierReportsController extends AppController {

    var $name = 'SupplierReports';
    var $pageTitle = 'Vendor Report';
    var $viewPath = 'suppliers/reports';
    var $uses = array('SDH', 'Product', 'CustProductSite', 'VSCustomer', 'UserCoH', 'Customer', 'Supplier', 'CustStockOutHead', 'Category', 'SiteCostCenter', 'Site');

    function beforeFilter() {
        parent::beforeFilter();
        $this->set('parentNav', 'supplier_reports');
    }

    function display() {

    }

    function _setSDHOrderOptions() {

        // เงื่อนไขใน Order by
        $this->set('orderOptions', array(
            'Product.supplier_part_no' => 'Vendor Part no.',
            'SUM(SD.price)' => 'Price',
            'frequency' => 'Frequency',
            'stock_in_qty' => 'Quantity',
            '(SD.price * SUM(SD.stock_in_qty))' => 'Total Price',
            '(Product.last_dispatch_date)' => 'Dispatch Date',
        ));

        // เรียงจากน้อยไปมาก หรือ มากไปน้อย
        $this->set('seqOptions', array(
            'asc' => 'Ascendent',
            'desc' => 'Descendent'
        ));
    }

    /**
     * หน้าหลัก report Customer Stock out
     */
    function dispatch_search() {
        $this->viewPath = 'suppliers/reports/dispatch';
        $this->pageTitle = 'Dispatch Report';

        $supplier_id = $this->Auth->user('supplier_id');

        $customers = $this->UserCoH->listCustomerOnHand($this->Auth->user('id'));
        $this->set('customers', $this->VSCustomer->find('list', array('conditions' => array('supplier_id' => $supplier_id, 'id' => $customers))));
        $this->set('categories', $this->Category->listCategoryBySupplierId($supplier_id));

        $this->_setSDHOrderOptions();
    }

    /**
     * function ประมวลผลเงื่อนไขการค้นหา Customer stock out
     */
    function dispatch_query() {
        $this->viewPath = 'suppliers/reports/dispatch';


        $sql = "SELECT Customer.cust_no, Customer.name, Site.site_no, Site.name,
                       CustProductSite.id, CustProductSite.stock_code,
                       Product.id, Product.supplier_part_no, Product.supplier_part_no,
                       COALESCE(CustProductSite.text, Product.text) text,
                       AVG(SD.price) price, SUM(SD.stock_in_qty) stock_in_qty, COUNT(*) frequency
                FROM   pre_products Product
                JOIN   pre_supplier_dispatches SD ON (Product.id = SD.product_id AND (SD.stock_in_qty > 0))
                JOIN   pre_supplier_dispatch_heads SDH ON (SD.supplier_dispatch_head_id = SDH.id AND (SDH.dispatch_status_id = " . DispatchStatus::CONSIGNED_STATUS . " OR SDH.dispatch_status_id = " . DispatchStatus::PARTIAL_CONSIGNED_STATUS . "))
                JOIN   pre_cust_products_sites CustProductSite ON (SD.cust_product_site_id = CustProductSite.id)
                JOIN   pre_customers Customer ON (Customer.id = SDH.customer_id)
                JOIN   pre_sites Site ON (Site.id = SDH.site_id)
                WHERE  SDH.supplier_id = {$this->Auth->user('supplier_id')} ";

        // เก็บ คำสั่ง SQL เอาไว้เป็นเงื่อนไขตอนดึง Stock out no หรือ remark
        $sql_conditions = '';
        // Check เงื่อนไขการค้นหา
        if (!empty($this->data['Product']['part_no'])) {
            $part_no = trim(strtoupper($this->data['Product']['part_no']));
            $str = " AND Product.supplier_part_no LIKE '%{$part_no}%'";
            $sql .= $str;
            $sql_conditions .= $str;
        }
        if (!empty($this->data['Product']['category_id'])) {
            $str = " AND Product.category_id = {$this->data['Product']['category_id']}";
            $sql .= $str;
            $sql_conditions .= $str;
        }
        if (!empty($this->data['Product']['supplier_id'])) {
            $str = " AND Product.supplier_id = {$this->data['Product']['supplier_id']}";
            $sql .= $str;
            $sql_conditions .= $str;
        }
        if (!empty($this->data['Product']['brand_id'])) {
            $str = " AND Product.brand_id = {$this->data['Product']['brand_id']}";
            $sql .= $str;
            $sql_conditions .= $str;
        }
        if (!empty($this->data['SDH']['dispatch_date_start'])) {
            $str = " AND SDH.dispatch_date BETWEEN '{$this->data['SDH']['dispatch_date_start']}' AND '{$this->data['SDH']['dispatch_date_end']}' ";
            $sql .= $str;
            $sql_conditions .= $str;
        }

        if (!empty($this->data['SDH']['customer_id'])) {
            $str = " AND SDH.customer_id = '{$this->data['SDH']['customer_id']}' ";
            $sql .= $str;
            $sql_conditions .= $str;
        }

        if (!empty($this->data['SDH']['site_id'])) {
            $str = " AND SDH.site_id = '{$this->data['SDH']['site_id']}' ";
            $sql .= $str;
            $sql_conditions .= $str;
        }

        if (!empty($this->data['SDH']['supplier_id'])) {
            $str = " AND SDH.supplier_id = '{$this->data['SDH']['supplier_id']}' ";
            $sql .= $str;
            $sql_conditions .= $str;
        }

        $order1 = !empty($this->data['Option']['order1']) ? $this->data['Option']['order1'] : 'Product.supplier_part_no';
        $order2 = !empty($this->data['Option']['order2']) ? $this->data['Option']['order2'] : null;
        $seq = !empty($this->data['Option']['seq']) ? $this->data['Option']['seq'] : 'ASC';

        $sql .= " GROUP BY Product.supplier_part_no, CustProductSite.stock_code, SD.price ORDER BY {$order1} {$seq}" . ($order2 != null ? ", {$order2} {$seq}" : '');

        if (!empty($this->data['Option']['limit'])) {
            $sql .= ' LIMIT ' . $this->data['Option']['limit'];
        }

        // sql ที่จะใช้ดึงรายการ stock out no และ remark
        $this->Session->write($this->name . 'sql', $sql_conditions);
        // set ตัวแปรให้หน้า view ว่าต้องโชว์ stock out no กับ remark หรือไม่
        $this->set('display_dispatch_no', $this->data['Option']['display_dispatch_no']);

        $this->data = $this->SDH->query($sql);
    }

    function dispatch_print() {
        $this->viewPath = 'suppliers/reports/dispatch';
        // ถ้ามีการค้นหาตาม Category ดึงชื่อ Category มาใส่ตัวแปรให้หน้า view
        if (!empty($this->data['Product']['category_id'])) {
            $this->Product->Category->id = $this->data['Product']['category_id'];
            $this->set('category_name', $this->Product->Category->field('name'));
        }

        if (!empty($this->data['Product']['supplier_id'])) {
            $this->Product->Supplier->id = $this->data['Product']['supplier_id'];
            $this->set('vendor_name', $this->Product->Supplier->field('name'));
        }

        if (!empty($this->data['Product']['brand_id'])) {
            $this->Product->Brand->id = $this->data['Product']['brand_id'];
            $this->set('brand_name', $this->Product->Brand->field('name'));
        }

        if (!empty($this->data['SDH']['customer_id'])) {
            $this->SDH->Customer->id = $this->data['SDH']['customer_id'];
            $this->set('customer_name', $this->SDH->Customer->field('name'));
        }

        if (!empty($this->data['SDH']['site_id'])) {
            $this->SDH->Site->id = $this->data['SDH']['site_id'];
            $this->set('site_name', $this->SDH->Site->field('name'));
        }

        $this->_setReportPrintNote();
        $this->_setSDHOrderOptions();

        $this->dispatch_query();
    }

    /**
     * สำหรับ Stock out report กรณีมีการแสดง stock out no
     */
    function get_dispatch_no($product_id, $cust_product_site_id) {
        $site_id = $this->Auth->user('site_id');
        $sql = "SELECT SDH.id, dispatch_no, stock_in_qty, DATE_FORMAT(dispatch_date, '%d %M %Y') dispatch_date
                FROM   pre_products Product
                JOIN   pre_supplier_dispatches SD ON (Product.id = SD.product_id AND (SD.stock_in_qty > 0))
                JOIN   pre_supplier_dispatch_heads SDH ON (SD.supplier_dispatch_head_id = SDH.id AND (SDH.dispatch_status_id = " . DispatchStatus::CONSIGNED_STATUS . " OR SDH.dispatch_status_id = " . DispatchStatus::PARTIAL_CONSIGNED_STATUS . "))
                WHERE  Product.id = {$product_id}
                AND    SDH.supplier_id = {$this->Auth->user('supplier_id')}
                AND    SD.cust_product_site_id = {$cust_product_site_id} ";

        // เอาเงื่อนไขจากการ search มาใส่เพื่ม
        $sql_condition = $this->Session->read($this->name . 'sql');
        $sql .= $sql_condition;

        $data = $this->CustProductSite->query($sql);
        return $data;
    }

    /**
     * กำหนดค่าตัวแปรให้แก่หน้า view
     */
    function _setReportPrintNote() {
        $this->layout = 'print';

        $this->set('report_date', date('d/m/Y'));
        $this->set('user', $this->Auth->user('name'));

        // เก็บเงื่อนไขที่ส่งมาลงตัวแปรให้หน้า view ตรวจสอบ
        $this->set('criteria', $this->data);
        $this->set('print_orientation', PRINT_LANDSCAPE);
    }

    function dispatch_excel() {
        $this->dispatch_print();
        parent::excel('VMI Vendor Dispatch Report');
    }

    function customer_reports() {
        $this->viewPath = 'suppliers/reports/customer_reports';
        $this->pageTitle = 'Customer Repoert';
//        $this->set('customers', $this->Customer->find('list', array('order' => 'name')));
    }

    function cust_stock_in_search() {
        $this->viewPath = 'suppliers/reports/customer_reports/stock_in';
        $this->pageTitle = 'Customer Stock in Report';

        $supplier_id = $this->Auth->user('supplier_id');
        $this->set('categories', $this->Category->listCategoryBySupplierId($supplier_id));
        $this->set('brands', $this->Product->Brand->find('list', array('order' => 'name')));
        $this->set('suppliers', $this->Product->Supplier->find('list', array('order' => 'name')));
        $customers = $this->UserCoH->listCustomerOnHand($this->Auth->user('id'));
        $this->set('customers', $this->VSCustomer->find('list', array('conditions' => array('supplier_id' => $this->Auth->user('supplier_id'), 'id' => $customers))));

        $this->setCustStockInOrderOptions();
    }

    function setCustStockInOrderOptions() {
        // เงื่อนไขใน Order by
        $this->set('orderOptions', array(
            'Product.supplier_part_no' => 'Vendor Part No.',
            'CustProductSite.stock_code' => 'Stock Code',
            'Customer.name' => 'Customer',
            'Site.name' => 'Site',
            'Supplier.name' => 'Vendor',
            'CustStockIn.quotation_price' => 'Price',
            'frequency' => 'Frequency',
            'stock_in_qty' => 'Quantity',
            '(CustStockIn.quotation_price * SUM(stock_in_qty))' => 'Total Price',
            '(CustProductSite.last_stock_in_date)' => 'Stock in Date',
        ));

        // เรียงจากน้อยไปมาก หรือ มากไปน้อย
        $this->set('seqOptions', array(
            'asc' => 'Ascendent',
            'desc' => 'Descendent'
        ));
    }

    /**
     * function ประมวลผลเงื่อนไขการค้นหา Customer stock in
     */
    function cust_stock_in_query() {
        $this->viewPath = 'suppliers/reports/customer_reports/stock_in';

        $limit = $this->data['Option']['limit'];

        $sql = "SELECT Product.id, CustProductSite.id, CustProductSite.stock_code, CustProductSite.dispatch_from, CustStockInHead.dispatch_from, Product.part_no, Product.supplier_part_no, Supplier.name,
                       AVG(CustStockIn.quotation_price) price, SUM(stock_in_qty) stock_in_qty, COUNT(*) frequency,
                       Category.name, COALESCE(CustProductSite.text, Product.text) text,
                       Customer.name, Site.name
                FROM pre_cust_products_sites CustProductSite JOIN pre_products Product ON (CustProductSite.product_id = Product.id)
                JOIN pre_categories Category ON (Category.id = Product.category_id)
                JOIN pre_cust_stocks_in CustStockIn ON (CustStockIn.cust_product_site_id = CustProductSite.id)
                JOIN pre_cust_stock_in_heads CustStockInHead ON (CustStockIn.cust_stock_in_head_id = CustStockInHead.id)
                JOIN pre_suppliers Supplier ON (CustProductSite.supplier_id = Supplier.id)
                JOIN pre_customers Customer ON (Customer.id = CustProductSite.customer_id)
                JOIN pre_sites Site ON (Site.id = CustProductSite.site_id)
                WHERE CustProductSite.discontinued = 0
                AND CustStockInHead.supplier_id = {$this->Auth->user('supplier_id')} ";

        // เก็บ คำสั่ง SQL เอาไว้เป็นเงื่อนไขตอนดึง Stock in no
        $sql_conditions = '';
        // Check เงื่อนไขการค้นหา
        if (!empty($this->data['Product']['part_no'])) {
            $part_no = trim(strtoupper($this->data['Product']['part_no']));
            $str = " AND Product.supplier_part_no LIKE '%{$part_no}%'";
            $sql .= $str;
            $sql_conditions .= $str;
        }
        if (!empty($this->data['Product']['category_id'])) {
            $str = " AND Product.category_id = {$this->data['Product']['category_id']}";
            $sql .= $str;
            $sql_conditions .= $str;
        }
        if (!empty($this->data['Product']['brand_id'])) {
            $str = " AND Product.brand_id = {$this->data['Product']['brand_id']}";
            $sql .= $str;
            $sql_conditions .= $str;
        }

        if (!empty($this->data['CustProductSite']['stock_code'])) {
            $stock_code = trim(strtoupper($this->data['CustProductSite']['stock_code']));
            $str = " AND CustProductSite.stock_code LIKE '%{$stock_code}%'";
            $sql .= $str;
            $sql_conditions .= $str;
        }

        if (!empty($this->data['CustProductSite']['customer_id'])) {
            $customer_id = $this->data['CustProductSite']['customer_id'];
            $str = " AND CustProductSite.customer_id = '{$customer_id}'";
            $sql .= $str;
            $sql_conditions .= $str;
        }

        if (!empty($this->data['CustProductSite']['site_id'])) {
            $site_id = $this->data['CustProductSite']['site_id'];
            $str = " AND CustProductSite.site_id = '{$site_id}'";
            $sql .= $str;
            $sql_conditions .= $str;
        }

        if (!empty($this->data['CustStockInHead']['stock_in_date_start'])) {
            $str = " AND CustStockInHead.stock_in_date BETWEEN '{$this->data['CustStockInHead']['stock_in_date_start']}' AND '{$this->data['CustStockInHead']['stock_in_date_end']}' ";
            $sql .= $str;
            $sql_conditions .= $str;
        }

        if (!empty($this->data['CustStockInHead']['dispatch_from'])) {
            $str = " AND CustStockInHead.dispatch_from = '{$this->data['CustStockInHead']['dispatch_from']}' ";
            $sql .= $str;
            $sql_conditions .= $str;
        }

        if (!empty($this->data['CustStockInHead']['supplier_id'])) {
            $str = " AND CustStockInHead.supplier_id = '{$this->data['CustStockInHead']['supplier_id']}' ";
            $sql .= $str;
            $sql_conditions .= $str;
        }

        $order1 = !empty($this->data['Option']['order1']) ? $this->data['Option']['order1'] : 'CustProductSite.stock_code';
        $order2 = !empty($this->data['Option']['order2']) ? $this->data['Option']['order2'] : 'CustProductSite.stock_code';
        $seq = !empty($this->data['Option']['seq']) ? $this->data['Option']['seq'] : 'ASC';

        $sql .= " GROUP BY CustProductSite.stock_code, CustStockIn.quotation_price ORDER BY {$order1} {$seq}" . ($order2 != null ? ", {$order2} {$seq}" : '');

        if (!empty($limit)) {
            $sql .= ' LIMIT ' . $limit;
        }

        // sql ที่จะใช้ดึงรายการ stock out no
        $this->Session->write($this->name . 'sql', $sql_conditions);
        // set ตัวแปรให้หน้า view ว่าต้องโชว์ stock out no หรือไม่
        $this->set('display_stock_in_no', $this->data['Option']['display_stock_in_no']);
        $this->data = $this->CustProductSite->query($sql);
    }

    /**
     * สำหรับ Stock out report กรณีมีการแสดง stock out no
     */
    function get_cust_stock_in_no($cust_product_site_id, $cust_stock_in_price) {
        $sql = "SELECT CustStockInHead.id, stock_in_no, stock_in_qty, DATE_FORMAT(stock_in_date, '%d %M %Y') stock_in_date
                FROM pre_cust_products_sites CustProductSite JOIN pre_products Product ON (CustProductSite.product_id = Product.id)
                JOIN pre_categories Category ON (Category.id = Product.category_id)
                JOIN pre_cust_stocks_in CustStockIn ON (CustStockIn.cust_product_site_id = CustProductSite.id)
                JOIN pre_cust_stock_in_heads CustStockInHead ON (CustStockIn.cust_stock_in_head_id = CustStockInHead.id)
                JOIN pre_suppliers Supplier ON (CustProductSite.supplier_id = Supplier.id)
                WHERE CustStockIn.quotation_price = {$cust_stock_in_price}
                AND CustStockInHead.supplier_id = {$this->Auth->user('supplier_id')}
                AND CustProductSite.id = {$cust_product_site_id} ";

        // เอาเงื่อนไขจากการ search มาใส่เพื่ม
        $sql_condition = $this->Session->read($this->name . 'sql');
        $sql .= $sql_condition;
//        debug($sql);
        $data = $this->CustProductSite->query($sql);
//        debug($data);
        return $data;
    }

    function cust_stock_in_print() {
        $this->viewPath = 'supplier/reports/customer_reports/stock_in';
        // ถ้ามีการค้นหาตาม Category ดึงชื่อ Category มาใส่ตัวแปรให้หน้า view
        if (!empty($this->data['Product']['category_id'])) {
            $this->CustProductSite->Product->Category->id = $this->data['Product']['category_id'];
            $this->set('category_name', $this->CustProductSite->Product->Category->field('name'));
        }

        if (!empty($this->data['CustProductSite']['customer_id'])) {
            $this->CustProductSite->Customer->id = $this->data['CustProductSite']['customer_id'];
            $this->set('customer_name', $this->CustProductSite->Customer->field('name'));
        }

        if (!empty($this->data['CustProductSite']['site_id'])) {
            $this->CustProductSite->Site->id = $this->data['CustProductSite']['site_id'];
            $this->set('site_name', $this->CustProductSite->Site->field('name'));
        }

        if (!empty($this->data['CustProductSite']['supplier_id'])) {
            $this->CustProductSite->Supplier->id = $this->data['CustProductSite']['supplier_id'];
            $this->set('supplier_name', $this->CustProductSite->Supplier->field('name'));
        }

        if (!empty($this->data['Product']['brand_id'])) {
            $this->CustProductSite->Product->Brand->id = $this->data['Product']['brand_id'];
            $this->set('brand_name', $this->CustProductSite->Product->Brand->field('name'));
        }

        if (!empty($this->data['CustStockInHead']['dispatch_from'])) {
            if ($this->data['CustStockInHead']['dispatch_from'] == ITS_PREFIX) {
                $this->set('vendor', $this->requestAction('system_preferences/get/company_name'));
            } else {
                if ($this->data['CustStockInHead']['supplier_id'] != null) {
                    $this->Supplier->id = $this->data['CustStockInHead']['supplier_id'];
                    $this->set('vendor', $this->Supplier->field('name'));
                } else {
                    $this->set('vendor', 'Other Vendor');
                }
            }
        }

        $this->setCustStockInOrderOptions();
        $this->_setReportPrintNote();
        $this->cust_stock_in_query();
    }

    function cust_stock_in_excel() {
        $this->cust_stock_in_print();
        parent::excel('VMI Vendor Customer Stock In Report');
    }

    /**
     * หน้าหลัก report Customer Stock out
     */
    function cust_stock_out_search() {
        $this->viewPath = 'suppliers/reports/customer_reports/stock_out';
        $this->pageTitle = 'Customer Stock out Report';
        $this->set('categories', $this->CustProductSite->Product->Category->find('list', array('order' => 'name')));

        $supplier_id = $this->Auth->user('supplier_id');
        $this->set('categories', $this->Category->listCategoryBySupplierId($supplier_id));
        $this->set('brands', $this->Product->Brand->find('list', array('order' => 'name')));
        $this->set('suppliers', $this->Product->Supplier->find('list', array('order' => 'name')));
        $customers = $this->UserCoH->listCustomerOnHand($this->Auth->user('id'));
        $this->set('customers', $this->VSCustomer->find('list', array('conditions' => array('supplier_id' => $this->Auth->user('supplier_id'), 'id' => $customers))));
    }

    /**
     * function ประมวลผลเงื่อนไขการค้นหา Customer stock out
     */
    function cust_stock_out_query() {
        $this->viewPath = 'suppliers/reports/customer_reports/stock_out';

        // set ตัวแปรให้หน้า view ว่าต้องโชว์ stock out no กับ remark หรือไม่
        $this->set('display_stock_out_no', $this->data['Option']['display_stock_out_no']);
        $this->data = $this->CustProductSite->reportCustStocksOutBySupplier($this->Auth->user('supplier_id'), $this->data);
    }

    function cust_stock_out_print() {
        $this->viewPath = 'suppliers/reports/customer_reports/stock_out';

        if (!empty($this->data['Product']['category_id'])) {
            $this->CustProductSite->Product->Category->id = $this->data['Product']['category_id'];
            $this->set('category_name', $this->CustProductSite->Product->Category->field('name'));
        }

        if (!empty($this->data['CustProductSite']['customer_id'])) {
            $this->CustProductSite->Customer->id = $this->data['CustProductSite']['customer_id'];
            $this->set('customer_name', $this->CustProductSite->Customer->field('name'));
        }

        if (!empty($this->data['CustProductSite']['site_id'])) {
            $this->CustProductSite->Site->id = $this->data['CustProductSite']['site_id'];
            $this->set('site_name', $this->CustProductSite->Site->field('name'));
        }

        if (!empty($this->data['CustProductSite']['supplier_id'])) {
            $this->CustProductSite->Supplier->id = $this->data['CustProductSite']['supplier_id'];
            $this->set('supplier_name', $this->CustProductSite->Supplier->field('name'));
        }

        if (!empty($this->data['Product']['brand_id'])) {
            $this->CustProductSite->Product->Brand->id = $this->data['Product']['brand_id'];
            $this->set('brand_name', $this->CustProductSite->Product->Brand->field('name'));
        }

        if (!empty($this->data['CustStockOutHead']['dispatch_from'])) {
            if ($this->data['CustStockOutHead']['dispatch_from'] == ITS_PREFIX) {
                $this->set('vendor', $this->requestAction('system_preferences/get/company_name'));
            } else {
                if ($this->data['CustStockOutHead']['supplier_id'] != null) {
                    $this->CustStockOutHead->Supplier->id = $this->data['CustStockOutHead']['supplier_id'];
                    $this->set('vendor', $this->CustStockOutHead->Supplier->field('name'));
                } else {
                    $this->set('vendor', 'Other Vendor');
                }
            }
        }

        $this->_setReportPrintNote();
        $this->cust_stock_out_query();
    }

    function cust_stock_out_excel() {
        $this->cust_stock_out_print();
        parent::excel('VMI Vendor Customer Stock Out Report');
    }

    function cust_stock_out_cost_center_search() {
        $this->viewPath = 'suppliers/reports/customer_reports/stock_out_cost_center';
        $this->pageTitle = 'Customer Stock out Report<br />Group by Cost Center';

        $customers = $this->UserCoH->listCustomerOnHand($this->Auth->user('id'));
        $this->set('customers', $this->VSCustomer->find('list', array('conditions' => array('supplier_id' => $this->Auth->user('supplier_id'), 'id' => $customers))));
    }

    function cust_stock_out_cost_center_query() {
        $this->viewPath = 'suppliers/reports/customer_reports/stock_out_cost_center';

        // set ตัวแปรให้หน้า view ว่าต้องโชว์ stock out no กับ remark หรือไม่
        $this->set('display_stock_out_no', $this->data['Option']['display_stock_out_no']);

        $supplier_id = $this->Auth->user('supplier_id');
        $this->data = $this->SiteCostCenter->reportSupplierCustStockOutByCostCenter($supplier_id, $this->data, $this->data['Option']['display_stock_out_no']);
    }

    function cust_stock_out_cost_center_print() {
        $this->viewPath = 'suppliers/reports/customer_reports/stock_out_cost_center';

        $this->Supplier->id = $this->Auth->user('supplier_id');
        $this->set('vendor_no', $this->Supplier->field('code'));
        $this->set('vendor_name', $this->Supplier->field('name'));

        // หาชื่อ customer และ site ที่ search เข้ามา
        $this->Customer->id = $this->data['CustProductSite']['customer_id'];
        $this->set('customer_no', $this->Customer->field('cust_no'));
        $this->set('customer_name', $this->Customer->field('name'));

        $this->Site->id = $this->data['CustProductSite']['site_id'];
        $this->set('site_no', $this->Site->field('site_no'));
        $this->set('site_name', $this->Site->field('name'));

        // ถ้ามีการค้นหาตาม CostCenter ดึงชื่อ CostCenter มาใส่ตัวแปรให้หน้า view
        if (!empty($this->data['SiteCostCenter']['id'])) {
            $this->SiteCostCenter->id = $this->data['SiteCostCenter']['id'];
            $this->set('cost_center_name', $this->SiteCostCenter->field('name'));
        }

        $this->_setReportPrintNote();

        $this->cust_stock_out_cost_center_query();
    }

    function cust_stock_out_cost_center_excel() {
        $this->cust_stock_out_cost_center_print();
        parent::excel('VMI Customer Stock Out Report Group by Cost Center');
    }

}

?>